﻿using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Book_Shop.DAL.DAO;

namespace Book_Shop.DAL.Gateway
{
    public class InventoryGateway:DbGateway
    {
        public List<Inventory> GetAllInventoryAtThisShop(Shop thisShop)
        {
            var inventories = new List<Inventory>();
            const string query = "SELECT * FROM Inventory WHERE Shop_Id=@shopId";
            Command.Parameters.Clear();
            Command.Parameters.Add("@shopId", SqlDbType.Int);
            Command.Parameters["@shopId"].Value = thisShop.Id;
            Command.CommandText = query;
            Connection.Open();
            using (SqlDataReader dataReader = Command.ExecuteReader())
            {
                while (dataReader.Read())
                {
                    Inventory inventory = new Inventory()
                                              {
                                                  Id = int.Parse(dataReader["Id"].ToString()),
                                                  TotalAmountOfBook =
                                                      int.Parse(dataReader["TotalAmountOfBook"].ToString())
                                              };
                    Book book = new Book {Id = int.Parse(dataReader["Book_Id"].ToString())};
                    inventory.Book = book;
                    inventory.Shop = thisShop;
                    inventories.Add(inventory);

                }
                Connection.Close();
                return inventories;
            }

        }

        public int GetAmountOfBooksAtThisShop(Book book,Shop thisShop)
        {
            Connection.Open();
            const string query = "SELECT GetAmountOfBook FROM Inventory WHERE Book_Id = @bookId and Shop_Id = @shopId";
            Command.CommandText = query;
            Command.Parameters.Clear();
            Command.Parameters.Add("@bookId", SqlDbType.Int);
            Command.Parameters.Add("@shopId", SqlDbType.Int);
            Command.Parameters["@bookId"].Value = book.Id;
            Command.Parameters["@shopId"].Value = thisShop.Id;
            int totalBooks=0;
            using (SqlDataReader dataReader = Command.ExecuteReader())
            {
                while (dataReader.Read())
                {
                    totalBooks = int.Parse(dataReader["TotalAmountOfBook"].ToString());
                }
            }
            Connection.Close();
            return totalBooks;
        }

        public void CheckOut(OrderSheet orderSheet)
        {
            
            const string query = "UPDATE Inventory SET TotalAmount= TotalAmount-@quantity WHERE Book_Id = @bookId AND Shop_Id =@shopId";
            Connection.Open();
            foreach (var book in orderSheet.Books)
            {
                Command.CommandText = query;
                Command.Parameters.Clear();
                Command.Parameters.Add("@quantity", SqlDbType.Int);
                Command.Parameters["@quantity"].Value = book.Qty;
                Command.Parameters.Add("@bookId", SqlDbType.Int);
                Command.Parameters["@bookId"].Value = book.Id;
                Command.Parameters.Add("@shopId", SqlDbType.Int);
                Command.Parameters["@shopId"].Value = orderSheet.Shop.Id;
                Command.ExecuteNonQuery();
            }
            Connection.Close();
           
        }
    }
}